//-----------------------------------------------------------------------
// <copyright file="a_Surveys_OptionDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_Surveys_OptionDAOBase
    {
        #region Common methods
        public virtual a_Surveys_Option Createa_Surveys_OptionFromReader(IDataReader reader)
        {
            a_Surveys_Option item = new a_Surveys_Option();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) item.ID = (int)reader["ID"];
                if (!reader.IsDBNull(reader.GetOrdinal("QID"))) item.QID = (int)reader["QID"];
                if (!reader.IsDBNull(reader.GetOrdinal("RespOption"))) item.RespOption = (string)reader["RespOption"];
                if (!reader.IsDBNull(reader.GetOrdinal("RespCount"))) item.RespCount = (int)reader["RespCount"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsOtherOption"))) item.IsOtherOption = (bool)reader["IsOtherOption"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Surveys_Option methods
            
        public virtual void Createa_Surveys_Option(a_Surveys_Option a_Surveys_Option)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_Options_ins");
                
                database.AddInParameter(dbCommand, "@QID", DbType.Int32, a_Surveys_Option.QID);
                database.AddInParameter(dbCommand, "@RespOption", DbType.String, a_Surveys_Option.RespOption);
                database.AddInParameter(dbCommand, "@RespCount", DbType.Int32, a_Surveys_Option.RespCount);
                database.AddInParameter(dbCommand, "@IsOtherOption", DbType.Boolean, a_Surveys_Option.IsOtherOption);
                database.AddOutParameter(dbCommand, "@ID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Surveys_Option.ID = (int)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Surveys_Option methods
        
        public virtual void Updatea_Surveys_Option(a_Surveys_Option a_Surveys_Option)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_Options_upd");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, a_Surveys_Option.ID);
                database.AddInParameter(dbCommand, "@QID", DbType.Int32, a_Surveys_Option.QID);
                database.AddInParameter(dbCommand, "@RespOption", DbType.String, a_Surveys_Option.RespOption);
                database.AddInParameter(dbCommand, "@RespCount", DbType.Int32, a_Surveys_Option.RespCount);
                database.AddInParameter(dbCommand, "@IsOtherOption", DbType.Boolean, a_Surveys_Option.IsOtherOption);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Surveys_Option methods
        public virtual void Deletea_Surveys_Option(int id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_Options_del");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Surveys_Option methods
        
        public virtual a_Surveys_Option Geta_Surveys_Option(int id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_Options_sel");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                a_Surveys_Option a_Surveys_Option =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Surveys_Option = Createa_Surveys_OptionFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Surveys_Option;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_Surveys_OptionList methods
        public virtual a_Surveys_OptionCollection Geta_Surveys_OptionList(a_Surveys_OptionColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_Options_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_Surveys_OptionCollection a_Surveys_OptionCollection = new a_Surveys_OptionCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Surveys_Option a_Surveys_Option = Createa_Surveys_OptionFromReader(reader);
                        a_Surveys_OptionCollection.Add(a_Surveys_Option);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_Surveys_OptionCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_Surveys_OptionCollection Geta_Surveys_OptionList(a_Surveys_OptionColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_Surveys_OptionList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion

        #region Geta_Surveys_OptionList_qid methods
        public virtual a_Surveys_OptionCollection Geta_Surveys_OptionList_qid(int qid, a_Surveys_OptionColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_Options_selList_qid");
                database.AddInParameter(dbCommand, "@QID", DbType.Int32, qid);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_Surveys_OptionCollection a_Surveys_OptionCollection = new a_Surveys_OptionCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Surveys_Option a_Surveys_Option = Createa_Surveys_OptionFromReader(reader);
                        a_Surveys_OptionCollection.Add(a_Surveys_Option);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_Surveys_OptionCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_Surveys_OptionCollection Geta_Surveys_OptionList_qid(int qid, a_Surveys_OptionColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_Surveys_OptionList_qid(qid, orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion

        public virtual a_Surveys_OptionCollection Geta_Surveys_OptionList(out string question, out int qid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_Options_selListByStatus");

                database.AddOutParameter(dbCommand, "@Question", DbType.String, 200);
                database.AddOutParameter(dbCommand, "@QID", DbType.Int32, 4);

                a_Surveys_OptionCollection a_Surveys_OptionCollection = new a_Surveys_OptionCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Surveys_Option a_Surveys_Option = Createa_Surveys_OptionFromReader(reader);
                        a_Surveys_OptionCollection.Add(a_Surveys_Option);
                    }
                    reader.Close();
                }
                question = database.GetParameterValue(dbCommand, "@Question").ToString();
                qid = (int)database.GetParameterValue(dbCommand, "@QID");
                return a_Surveys_OptionCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region Geta_Surveys_OptionList_alphabet methods
        public virtual a_Surveys_OptionCollection Geta_Surveys_OptionList_alphabet(int qid, string mychar, a_Surveys_OptionColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Surveys_Options_selList_alphabet");
                database.AddInParameter(dbCommand, "@Mychar", DbType.AnsiString, mychar);
                database.AddInParameter(dbCommand, "@QID", DbType.Int32, qid);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_Surveys_OptionCollection a_Surveys_OptionCollection = new a_Surveys_OptionCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Surveys_Option a_Surveys_Option = Createa_Surveys_OptionFromReader(reader);
                        a_Surveys_OptionCollection.Add(a_Surveys_Option);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_Surveys_OptionCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_Surveys_OptionCollection Geta_Surveys_OptionList_alphabet(int qid, string mychar, a_Surveys_OptionColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_Surveys_OptionList_alphabet(qid, mychar, orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion
    }
}